package com.xique.door.project.elevator.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.OrikaUtil;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.*;
import com.xique.door.bean.request.elevator.ElevatorComElvInfoListWebRequest;
import com.xique.door.bean.request.elevator.ElevatorLastRunListWebRequest;
import com.xique.door.bean.request.elevator.ElevatorRunInfoWebRequest;
import com.xique.door.bean.request.elevator.ElevatorTotalMeterWebRequest;
import com.xique.door.bean.response.IotApiResponse;
import com.xique.door.bean.search.ElevatorListSearch;
import com.xique.door.bean.search.ElevatorRunListSearch;
import com.xique.door.bean.vo.elevator.*;
import com.xique.door.constant.ElevatorConstant;
import com.xique.door.project.elevator.biz.service.IElevatorBizWebService;
import com.xique.door.service.*;
import com.xique.door.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shd
 * @create 2021/10/15 13:21
 */
@Service
public class IElevatorBizWebServiceImpl implements IElevatorBizWebService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IElevatorService elevatorService;

    @Autowired
    private IIotApiService iotApiService;

    @Autowired
    private IElevatorParamService elevatorParamService;

    @Autowired
    private IElevatorReportService elevatorReportService;

    @Autowired
    private IElevatorRunService elevatorRunService;

    @Autowired
    private IElevatorEventService elevatorEventService;

    @Autowired
    private IElevatorFaultService elevatorFaultService;

    @Autowired
    private IAreaService areaService;

    @Override
    public AjaxResult getSessionToken() {
        IotApiResponse response = iotApiService.getMd5Token();
        if (ElevatorConstant.ResponseCode.SUCCESS != response.getResponse_code()) {
            return AjaxResult.error(response.getResponse_msg());
        }
        String md5 = response.getResp().toString();
        response = iotApiService.getAccessToken(md5);
        if (ElevatorConstant.ResponseCode.SUCCESS != response.getResponse_code()) {
            return AjaxResult.error(response.getResponse_msg());
        }
        JSONObject resp = JSONObject.parseObject(JSON.toJSONString(response.getResponse_data()));
        String accessToken = resp.get("access_token").toString();
        response = iotApiService.getSessionToken(accessToken);
        if (ElevatorConstant.ResponseCode.SUCCESS != response.getResponse_code()) {
            return AjaxResult.error(response.getResponse_msg());
        }
        String sessionToken = response.getResp().toString();
        redisUtil.set(ElevatorConstant.IotSessionTokenRedisKey, sessionToken, 2 * 60 * 60 - 3);
        return AjaxResult.success("成功", accessToken);
    }

    @Override
    public AjaxResult getCommunityData(Long parkId) {

        Elevator search = new Elevator();
        search.setParkId(parkId);
        search.setStatus("normal");
        List<Elevator> elevatorList = elevatorService.selectElevatorList(search);

        List<Long> elvIds = elevatorList.stream().map(Elevator::getId).collect(Collectors.toList());
        List<ElevatorFaultCountStatisticVO> faultCountStatisticVOS = elevatorFaultService.selectElevatorFaultCountsByElvIdsInMonth(elvIds);
        Map<Long, Integer> faultCountStatisticMap = faultCountStatisticVOS.stream().collect(Collectors.toMap(ElevatorFaultCountStatisticVO::getElvId, ElevatorFaultCountStatisticVO::getCount));

        List<ElevatorInfoListWebVO> vos = OrikaUtil.converts(elevatorList, ElevatorInfoListWebVO.class);
        vos.forEach(item -> {
            if (null != faultCountStatisticMap.get(item.getId())) {
                item.setFaultCount(faultCountStatisticMap.get(item.getId()));
            } else {
                item.setFaultCount(0);
            }
        });

        ElevatorCommunityInfo info = elevatorService.selectElevatorCommunityInfoByParkId(parkId);

        ElevatorInfoWebVO vo = OrikaUtil.convert(info, ElevatorInfoWebVO.class);
        vo.setParkId(info.getId());
        vo.setElevators(vos);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getDeviceRunInfo(Long elvId) {

        Elevator elevator = elevatorService.selectElevatorById(elvId);
        String imei = elevator.getImeiCode();

        IotApiResponse response = iotApiService.getDeviceRunInfo(imei);
        if (ElevatorConstant.ResponseCode.SUCCESS != response.getResponse_code()) {
            return AjaxResult.error(response.getResponse_msg());
        }

        return AjaxResult.success(response.getResp());
    }

    @Override
    public AjaxResult getElevatorParameter(Long elvId) {

        ElevatorParam param = elevatorParamService.selectElevatorParamByElvId(elvId);

        if (null == param) {
            return AjaxResult.error("未找到对应的数据");
        }

        return AjaxResult.success(param);
    }

    @Override
    public AjaxResult getElvInfo(Long elvId) {

        Elevator elevator = elevatorService.selectElevatorById(elvId);

        if (null == elevator) {
            return AjaxResult.error("未找到对应的数据");
        }

        return AjaxResult.success(elevator);
    }

    @Override
    public AjaxResult getElvRun(ElevatorRunInfoWebRequest request) {

        Long parkId = request.getParkId();
        Long elvId = request.getElvId();

        ElevatorReport report = elevatorReportService.selectElevatorReportByElvId(elvId);

        // 设备总量
        Integer deviceCount = elevatorService.selectElevatorCountByParkId(parkId);

        ElevatorRunInfoWebVO vo = new ElevatorRunInfoWebVO();

        vo.setRunFloor(report.getRunFloor());
        vo.setRunHour(report.getRunHour());
        vo.setRunMeter(report.getRunMeter());
        vo.setRunTimes(report.getRunTimes());
        vo.setDeviceCount(deviceCount);
        vo.setOpenAndCloseTimes(report.getOpenCloseTimes());
        vo.setUpTimes(report.getUpTimes());
        vo.setUpHour(String.format("%.3f", report.getUpHour()));
        vo.setDownHour(String.format("%.3f", report.getDownHour()));
        vo.setDownTimes(report.getDownTimes());
        vo.setOpenHour(String.format("%.3f", report.getOpenHour()));
        vo.setOpenTimes(report.getOpenTimes());
        vo.setCloseHour(String.format("%.3f", report.getCloseHour()));
        vo.setCloseTimes(report.getCloseTimes());

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getElvFrequentPeriod(Long elvId) {
        Map<String, Object> map = new HashMap<>(6);
        List<String> x = Arrays.asList("0-4", "4-8", "8-12", "12-16", "16-20", "20-24");
        map.put("x", x);

        List<ElevatorEventStatisticVO> voList = elevatorEventService.selectElevatorEventStatisticByElvId(elvId);
        List<Integer> y = voList.stream().map(ElevatorEventStatisticVO::getCount).collect(Collectors.toList());
        map.put("y", y);

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getTotalMeter(ElevatorTotalMeterWebRequest request) {

        Long elvId = request.getElvId();

        List<String> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        if ("week".equals(request.getType())) {
            // 星期
            for (int i = 7; i > 0; i--) {
                x.add(DateUtils.dateToWeek(DateUtils.getPrevioudDate(i - 1), new String[]{"日", "一", "二", "三", "四", "五", "六"}));
            }
            List<ElevatorRunStatisticVO> voList = elevatorRunService.selectElevatorRunMeterStatisticByWeek(elvId);
            y = voList.stream().map(ElevatorRunStatisticVO::getTotal).collect(Collectors.toList());
            if (y.size() < 7) {
                int count = y.size();
                for (int i = 0; i < 7 - count; i++) {
                    y.add(0,0);
                }
            }
        }

        if ("month".equals(request.getType())) {
            // 月
            for (int i = 30; i > 0; i--) {
                x.add(String.valueOf(Integer.parseInt(StringUtils.substring(DateUtils.getPrevioudDate(i - 1), 8, 10))));
            }
            List<ElevatorRunStatisticVO> voList = elevatorRunService.selectElevatorRunMeterStatisticByMonth(elvId);
            y = voList.stream().map(ElevatorRunStatisticVO::getTotal).collect(Collectors.toList());
            if (y.size() < 30) {
                int count = y.size();
                for (int i = 0; i < 30 - count; i++) {
                    y.add(0,0);
                }
            }
        }

        if ("year".equals(request.getType())) {
            // 年
            for (int i = 12; i > 0; i--) {
                x.add(String.valueOf(Integer.parseInt(StringUtils.substring(DateUtils.getLastMonths(i - 1), 5, 7))));
            }
            List<ElevatorRunStatisticVO> voList = elevatorRunService.selectElevatorRunMeterStatisticByYear(elvId);
            y = voList.stream().map(ElevatorRunStatisticVO::getTotal).collect(Collectors.toList());
            if (y.size() < 12) {
                int count = y.size();
                for (int i = 0; i < 12 - count; i++) {
                    y.add(0,0);
                }
            }
        }

        Map<String, Object> map = new HashMap<>(2);
        map.put("x", x);
        map.put("y", y);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getElvWaitTimeAndArrivalTimes(Long elvId) {

        ElevatorParam param = elevatorParamService.selectElevatorParamByElvId(elvId);

        if (null == param) {
            return AjaxResult.error("未找到对应数据");
        }

        Integer floor = param.getMaxFloor();

        ElevatorRunListSearch search = new ElevatorRunListSearch();
        search.setElvId(elvId);
        search.setEndFloorNotEqual("未知楼层");
        List<ElevatorRun> list = elevatorRunService.selectElevatorRunListNew(search);

        int count = floor / 5;

        List<String> x = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            x.add((count * i + 1) + "-" + (i == 4 ? floor : count * (i + 1)));
        }

        List<Object> y = Arrays.asList(0, 0, 0, 0, 0);
        List<Object> yArrival = Arrays.asList(0, 0, 0, 0, 0);
        for (ElevatorRun item : list) {
            double wait = item.getWaitTime();
            if (Integer.parseInt(item.getEndFloor()) > 0 && Integer.parseInt(item.getEndFloor()) <= count) {
                y.set(0, waitTime(y, 0, wait));
                yArrival.set(0, arrivalTimes(yArrival, 0));
            }
            if (Integer.parseInt(item.getEndFloor()) > count && Integer.parseInt(item.getEndFloor()) <= count * 2) {
                y.set(1, waitTime(y, 1, wait));
                yArrival.set(1, arrivalTimes(yArrival, 1));
            }
            if (Integer.parseInt(item.getEndFloor()) > (count * 2) && Integer.parseInt(item.getEndFloor()) <= (count * 3)) {
                y.set(2, waitTime(y, 2, wait));
                yArrival.set(2, arrivalTimes(yArrival, 2));
            }
            if (Integer.parseInt(item.getEndFloor()) > (count * 3) && Integer.parseInt(item.getEndFloor()) <= (count * 4)) {
                y.set(3, waitTime(y, 3, wait));
                yArrival.set(3, arrivalTimes(yArrival, 3));
            }
            if (Integer.parseInt(item.getEndFloor()) > (count * 4) && Integer.parseInt(item.getEndFloor()) <= floor) {
                y.set(4, waitTime(y, 4, wait));
                yArrival.set(4, arrivalTimes(yArrival, 4));
            }
        }

        Map<String, Object> map = new HashMap<>(2);
        map.put("x", x);
        map.put("y", y);
        map.put("yArrival", yArrival);
        return AjaxResult.success(map);
    }

    private double waitTime(List<Object> list, int index, double wait) {
        if (null != list.get(index)) {
            return wait + Double.parseDouble(list.get(index).toString());
        } else {
            return wait;
        }
    }

    private int arrivalTimes(List<Object> list, int index) {
        if (null != list.get(index)) {
            return Integer.parseInt(list.get(index).toString()) + 1;
        } else {
            return 1;
        }
    }

    @Override
    public AjaxResult getElvEventList(ElevatorLastRunListWebRequest request) {

        Map<String, Object> map = new HashMap<>(2);

        if (StringUtils.isNotEmpty(request.getFlag()) || (null != request.getLevel() && request.getLevel() > 0)) {
            // 故障
            ElevatorFault elevatorFaultSearch = new ElevatorFault();
            elevatorFaultSearch.setEleId(request.getElvId());
            elevatorFaultSearch.setStartDate(request.getTime());
            elevatorFaultSearch.setFaultName(request.getName());
            elevatorFaultSearch.setFaultLevel(request.getLevel());
            elevatorFaultSearch.setFlag(request.getFlag());
            PageHelper.startPage(request.getPageNum(), request.getPageSize(),StringUtils.toUnderScoreCase("end_date") + " " + "desc");
            List<ElevatorFault> elevatorFaultList = elevatorFaultService.selectElevatorFaultList(elevatorFaultSearch);
            List<ElevatorLastRunListWebVO> vos = new ArrayList<>();
            elevatorFaultList.forEach(item -> {
                ElevatorLastRunListWebVO vo = new ElevatorLastRunListWebVO();
                vo.setName(item.getFaultName());
                vo.setFlag(item.getFlag());
                vo.setLevel(item.getFaultLevel());
                vo.setStartTime(item.getStartDate());

                vos.add(vo);
            });

            map.put("list", vos);
            map.put("count", new PageInfo(elevatorFaultList).getTotal());

            return AjaxResult.success(map);
        }

        ElevatorRun search = new ElevatorRun();
        search.setEleId(request.getElvId());
        search.setStartTime(request.getTime());
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),StringUtils.toUnderScoreCase("end_time") + " " + "desc");
        List<ElevatorRun> list = elevatorRunService.selectElevatorRunList(search);

        List<ElevatorLastRunListWebVO> vos = OrikaUtil.converts(list, ElevatorLastRunListWebVO.class);

        map.put("list", vos);
        map.put("count", new PageInfo(list).getTotal());

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getElvLastEvent(ElevatorLastRunListWebRequest request) {

        ElevatorRun search = new ElevatorRun();
        search.setEleId(request.getElvId());
        PageHelper.startPage(1, 20,StringUtils.toUnderScoreCase("end_time") + " " + "desc");
        List<ElevatorRun> list = elevatorRunService.selectElevatorRunList(search);

        List<ElevatorLastRunListWebVO> vos = OrikaUtil.converts(list, ElevatorLastRunListWebVO.class);

        ElevatorFault elevatorFaultSearch = new ElevatorFault();
        elevatorFaultSearch.setEleId(request.getElvId());
        PageHelper.startPage(1, 10,StringUtils.toUnderScoreCase("end_date") + " " + "desc");
        List<ElevatorFault> elevatorFaultList = elevatorFaultService.selectElevatorFaultList(elevatorFaultSearch);
        List<ElevatorLastRunListWebVO> finalVos = new ArrayList<>();
        elevatorFaultList.forEach(item -> {
            ElevatorLastRunListWebVO vo = new ElevatorLastRunListWebVO();
            vo.setName(item.getFaultName());
            vo.setFlag(item.getFlag());
            vo.setLevel(item.getFaultLevel());
            vo.setStartTime(item.getStartDate());

            finalVos.add(vo);
        });
        vos.addAll(finalVos);

        vos = vos.stream().sorted(Comparator.comparing(ElevatorLastRunListWebVO::getStartTime).reversed()).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>(1);
        map.put("list", vos);

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getServiceInfo(Long parkId) {

        ElevatorServiceInfoStatisticVO vo = elevatorService.selectElevatorServiceInfoCountByParkId(parkId);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getAreaServiceInfo(Long code) {

        ElevatorServiceInfoStatisticVO vo = elevatorService.selectElevatorServiceInfoCountByAddressCode(code);

        if (null == vo) {
            vo = new ElevatorServiceInfoStatisticVO();
        }

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getSecurityRanking(Long code) {

        List<String> list = elevatorService.selectElevatorSecurityRankingByAddressCode(code);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult getComElvRun(Long parkId) {

        List<ElevatorReport> reportList = elevatorReportService.selectElevatorReportListByParkId(parkId);
        Long runTimes = 0L;
        Double runMeter = 0.0;
        Long runHour = 0L;
        Long runFloor = 0L;
        Long openCloseTimes = 0L;
        for (ElevatorReport item : reportList) {
            runHour += item.getRunHour();
            runMeter += item.getRunMeter();
            runTimes += item.getRunTimes();
            runFloor += (null == item.getRunFloor() ? 0 : item.getRunFloor());
            openCloseTimes += item.getOpenCloseTimes();
        }

        // 设备总量
        int deviceCount = elevatorService.selectElevatorCountByParkId(parkId);

        ElevatorRunInfoWebVO vo = new ElevatorRunInfoWebVO();

        vo.setRunFloor(runFloor);
        vo.setRunHour(runHour);
        vo.setRunMeter(runMeter.longValue());
        vo.setRunTimes(runTimes);
        vo.setDeviceCount(deviceCount);
        vo.setOpenAndCloseTimes(openCloseTimes);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getComElvInfo(ElevatorComElvInfoListWebRequest request) {

        ElevatorListSearch search = new ElevatorListSearch();
        search.setParkId(request.getParkId());
        search.setContent(request.getSearch());
        search.setStatus("normal");
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),null);
        List<ElevatorInfoListWebVO> list = elevatorService.selectElevatorList(search);
        long count = new PageInfo(list).getTotal();

        List<Long> elvIds = list.stream().map(ElevatorInfoListWebVO::getId).collect(Collectors.toList());
        List<ElevatorFaultCountStatisticVO> faultCountStatisticVOS = elevatorFaultService.selectElevatorFaultCountsByElvIdsInMonth(elvIds);
        Map<Long, Integer> faultCountStatisticMap = faultCountStatisticVOS.stream().collect(Collectors.toMap(ElevatorFaultCountStatisticVO::getElvId, ElevatorFaultCountStatisticVO::getCount));

        list.forEach(item -> {
            if (null != faultCountStatisticMap.get(item.getId())) {
                item.setFaultCount(faultCountStatisticMap.get(item.getId()));
            } else {
                item.setFaultCount(0);
            }
        });

        Map<String, Object> map = new HashMap<>(2);
        map.put("list", list);
        map.put("count", count);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getBaseData() {

        List<Elevator> elevatorList = elevatorService.selectElevatorByAddressCode(330600L);
        List<Long> parkIds = elevatorList.stream().map(Elevator::getParkId).distinct().collect(Collectors.toList());

        List<ElevatorCommunityInfo> communityInfoList = elevatorService.selectElevatorCommunityInfoByParkIds(parkIds);

        Map<Long, List<ElevatorCommunityInfo>> districtMap = communityInfoList.stream().collect(Collectors.toMap(
                ElevatorCommunityInfo::getDistrictId,
                Arrays::asList,
                (List<ElevatorCommunityInfo> oldList, List<ElevatorCommunityInfo> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }));

        List<ElevatorCommunityVO> districtVOs = new ArrayList<>();
        for (Long code : districtMap.keySet()) {
            ElevatorCommunityVO vo = new ElevatorCommunityVO();
            List<ElevatorCommunityInfo> infoList = districtMap.get(code);
            vo.setCode(code);
            vo.setName(infoList.get(0).getDistrictName());
            vo.setCommunity(infoList);
            districtVOs.add(vo);
        }

        ElevatorCommunityVO vo = new ElevatorCommunityVO();
        vo.setCode(330600L);
        vo.setName("绍兴市");
        vo.setChild(districtVOs);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getRegionInfo(Long code) {

        Area area = areaService.selectAreaByCode(code.toString());

        Area search = new Area();
        search.setParentId(area.getAreaId());
        List<Area> list = areaService.selectAreaList(search);

        List<String> codes = list.stream().map(Area::getAreaCode).collect(Collectors.toList());

        List<ElevatorCountInAreaStatisticVO> vos = elevatorService.selectElevatorCountByAddressCodes(codes, area.getLevel() + 1);

        Map<String, Integer> map = new HashMap<>();
        if (1 == area.getLevel()) {
            map = vos.stream().collect(Collectors.toMap(ElevatorCountInAreaStatisticVO::getCity, ElevatorCountInAreaStatisticVO::getCount));
        }
        if (2 == area.getLevel()) {
            map = vos.stream().collect(Collectors.toMap(ElevatorCountInAreaStatisticVO::getDistrict, ElevatorCountInAreaStatisticVO::getCount));
        }

        List<ElevatorAreaVO> voList = OrikaUtil.converts(list, ElevatorAreaVO.class);
        Map<String, Integer> finalMap = map;
        voList.forEach(item -> {
            item.setCount(null == finalMap.get(item.getAreaCode()) ? 0 : finalMap.get(item.getAreaCode()));
        });

        return AjaxResult.success(Collections.singletonMap("list", voList));
    }

    @Override
    public AjaxResult getRegionCount(Long code) {

        int deviceCount = elevatorService.selectElevatorCountByAddressCode(code);

        return AjaxResult.success(deviceCount);
    }

    @Override
    public AjaxResult getCommunity(Long code) {

        List<Elevator> elevatorList = elevatorService.selectElevatorByAddressCode(code);
        List<Long> parkIds = elevatorList.stream().map(Elevator::getParkId).distinct().collect(Collectors.toList());

        List<ElevatorCommunityInfo> list = elevatorService.selectElevatorCommunityInfoByParkIds(parkIds);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult searchCommunity(String content) {

        if (StringUtils.isEmpty(content)) {
            return AjaxResult.success(Collections.singletonMap("list", new ArrayList<>()));
        }

        List<ElevatorCommunityInfo> list = elevatorService.selectElevatorCommunityInfoBySearch(content);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult getFaultList(Long code) {

        List<ElevatorFaultVO> list = elevatorFaultService.selectElevatorFaultsByAddressCodeInMonth(code);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult getComFaultList(Long parkId) {

        List<ElevatorFaultVO> list = elevatorFaultService.selectElevatorFaultsByParkIdInMonth(parkId);

        return AjaxResult.success(Collections.singletonMap("list", list));
    }

    @Override
    public AjaxResult getGeneralData(Long code) {

        ElevatorGeneralVO vo = elevatorService.selectElevatorGeneralByAddressCode(code);

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getCityElvRun(Long code) {

        ElevatorRunDataStatisticVO elevatorRunDataStatisticVO = elevatorReportService.selectElevatorRunDataByAddressCode(code);

        // 设备总量
        int deviceCount = elevatorService.selectElevatorCountByAddressCode(code);

        ElevatorRunInfoWebVO vo = new ElevatorRunInfoWebVO();

        vo.setRunFloor(elevatorRunDataStatisticVO.getRunFloor());
        vo.setRunHour(elevatorRunDataStatisticVO.getRunHour());
        vo.setRunMeter(elevatorRunDataStatisticVO.getRunMeter().longValue());
        vo.setRunTimes(elevatorRunDataStatisticVO.getRunTimes());
        vo.setDeviceCount(deviceCount);
        vo.setOpenAndCloseTimes(elevatorRunDataStatisticVO.getOpenCloseTimes());

        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult getAreaFault(Long code) {

        Long times = elevatorFaultService.selectElevatorFaultTimesStatisticByAddressCode(code);

        Integer count = elevatorFaultService.selectElevatorFaultDeviceCountStatisticByAddressCode(code);

        List<Long> day = elevatorFaultService.selectElevatorFaultDayStatisticByAddressCode(code);

        long total = day.stream().mapToLong(item -> StringUtils.isNull(item) ? 0L : item).sum();

        Map<String, Object> map = new HashMap<>(3);
        map.put("times", times);
        map.put("deviceCount", count);
        map.put("day", String.format("%.2f", (double) total / (60 * 60 * 24)));

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getElectromobileInfo() {

        List<ElevatorFaultVO> list = elevatorFaultService.selectElevatorLastElectromobileFaultList();

        return AjaxResult.success(Collections.singletonMap("list", list));
    }
}
